package com.hlw.hr_man.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlw.hr_man.Model.Absent;
import com.hlw.hr_man.service.AbsentService;
import com.hlw.hr_man.serviceImpl.AbsentServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: GetCurrentEmployeeAbsentController
 * @Description: 处理获取当前员工缺勤记录操作
 * @author 唐明
 * @date 2016年9月20日 上午10:09:30
 *
 */
public class GetCurrentEmployeeAbsentController extends HttpServlet{

	/**
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */ 
	private static final long serialVersionUID = 1L;
	
	AbsentService aService = new AbsentServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		//获得当前用户id
		int eid = (int) req.getSession().getAttribute("employeeid");
		//获得当前页码
		int page = Integer.parseInt(req.getParameter("page"));
		//获得每页行数
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		//计算开始页码
		int start = (page-1)*pageSize;
		//获得当前用户的缺勤列表
		List<Absent> absents = aService.getEmployeeByEid(eid, start, pageSize);
		//获得当前用户缺勤记录总数
		int absent_count = aService.getEmployeeAbsent(eid);
		//将缺勤记录以json格式存放
		JSONObject currentEmpAbsents = new JSONObject();
		currentEmpAbsents.put("total", absent_count);
		currentEmpAbsents.put("rows", JSONArray.fromObject(absents));
		//将数据传回页面
		PrintWriter pw = resp.getWriter();
		pw.write(currentEmpAbsents.toString());
	}
}
